package net.kesumu.yushabu.testing;

import java.util.ArrayList;
import java.util.List;
 




import net.kesumu.yushabuserver.entity.Chat;
import net.kesumu.yushabuserver.entity.Message;

import net.kesumu.yushabu.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 

/**
 * implement the custom list view adapter class
 * The original class I got from url below:
 * http://www.androidhive.info/2014/10/android-building-group-chat-app-using-sockets-part-2/
 * @author Z 2014-12-19
 *
 */
public class MessagesListAdapter extends BaseAdapter {
 
    private Context context;
    private List<Message> messagesItems = new ArrayList<Message>();
 
    public MessagesListAdapter(Context context, List<Message> navDrawerItems) {
        this.context = context;
        this.messagesItems = navDrawerItems;
    }
    
    public void setData(List<Message> cl) {
        this.messagesItems = cl;
        this.notifyDataSetChanged();
    }
 
    public int getCount() {
        return messagesItems.size();
    }
 
    public Object getItem(int position) {
        return messagesItems.get(position);
    }
 
    public long getItemId(int position) {
        return position;
    }
 
    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup parent) {
 
        /**
         * The following list not implemented reusable list items as list items
         * are showing incorrect data Add the solution if you have one
         * */
 
        Message m = messagesItems.get(position);
 
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
 
        // Identifying the message owner
        if (messagesItems.get(position).isSelf()) {
            // message belongs to you, so load the right aligned layout
            convertView = mInflater.inflate(R.layout.list_item_message_right,
                    null);
        } else {
            // message belongs to other person, load the left aligned layout
            convertView = mInflater.inflate(R.layout.list_item_message_left,
                    null);
        }
 
//        TextView lblFrom = (TextView) convertView.findViewById(R.id.lblMsgFrom);
//        lblFrom.setText(m.getFromName());
        
        TextView txtMsg = (TextView) convertView.findViewById(R.id.txtMsg);
        txtMsg.setText(m.getMessage());
        
 
        return convertView;
    }
    
    public void addItem(final Message item) {
        messagesItems.add(item);
        notifyDataSetChanged();
    }
}
